/**
 * @author Tin
 * @version 0.1.4
 */
jQuery(function($) {
    "use strict";
    var clientName, clientTable;

    clientName = "tin";
    clientTable = $("<table />", {
        class : "tabular"
    }).appendTo($("<div />", {
        class : "tabular-container"
    }).insertAfter($("body").children().not("script"))).asTable({});

    $.each({
        0 : [ "section", "Draft Candidate Section" ],
        1 : [ "status", "Status" ],
        2 : [ "owner", "Owner" ],
        3 : [ "qa", "Quality Assurance (QA)" ],
        4 : [ "due-date", "Expected Completion" ],
        5 : [ "text-notes", "Notes" ]
    }, function(key, fieldProperties) {
        clientTable.tableAdd({
            fieldName : fieldProperties[0],
            displayName : fieldProperties[1]
        });
    });

    $.ajax({
        url : "/trac",
        type : "GET",
        dataType : "json",
        data : {
            client_name : clientName
        }
    }).done(function(response, textStatus, jqXHR) {
        var data, parseStatus = function(statusCode) {
            switch (statusCode) {
                case 0:
                    return "Inactive";
                case 1:
                    return "Active";
                default:
                    return undefined;
            }
        };
        if (response && response.d) data = response.d;
        else return false;
        if (data.clientName === clientName) {
            $.each(data.sections, function(i, section) {
                section[1] = parseStatus(section[1]);
                clientTable.asTable("insert", {
                    data : section,
                    after : function() {
                        console.log("Added", this);
                    }
                });
            });
        }
    });

    clientTable.hide().delay(500).show("slide", {
        direction : "up"
    }, 750);
});